#一维数据练习
score<-c(1:10)
group=c("红","绿","绿","蓝","红",
        "红","蓝","蓝","绿","绿")
tapply(score,group,mean)

#二维数据练习
data1=sample(80:100,10)
data2=sample(60:70,10)
dataframe=data.frame(data1,data2,score,group)
print(dataframe)

#数据合并
m1=rbind(score,group)#纵向合并
m2=rbind(m1,data1)
m3=rbind(m2,data2)
print(m3)
n1=cbind(score,group)#横向合并
n2=cbind(n1,data1)
n3=cbind(n2,data2)
print(n3)

#文件读入读出
install.packages("openxlsx")#安装openxlsx包
library("openxlsx")#调用包
#读取文件
d1=read.delim("C:\\Users\\86183\\Desktop\\data for class3\\ADdata1.txt")
d2=read.csv("C:\\Users\\86183\\Desktop\\data for class3\\ADdata2.csv")
d3=read.xlsx("C:\\Users\\86183\\Desktop\\data for class3\\ADdata3.xlsx")
d4=read.table("C:\\Users\\86183\\Desktop\\data for class3\\ADdata4.txt")
#以下将每个表中的蛋白名称列取名为"Protein.IDs"
names(d2)[1]="Protein.IDs"#csv文件可直接取第一列
print(d2)
names(d3)[1]="Protein.IDs"#xlsx文件可直接取第一列
print(d3)
nd1=cbind(d1,rownames(d1))
names(nd1)[17]="Protein.IDs"#txt文件在最后一列后加上txt的索引列，即蛋白名称列
print(nd1)
nd4=cbind(d4,rownames(d4))
names(nd4)[17]="Protein.IDs"#txt文件在最后一列后加上txt的索引列，即蛋白名称列
print(nd4)
#使用merge合并
result1=merge(nd1,d2,by.x="Protein.IDs",by.y="Protein.IDs")
result2=merge(d3,nd4,by.x="Protein.IDs",by.y="Protein.IDs")
result=merge(result1,result2,by.x="Protein.IDs",by.y="Protein.IDs")
print(result)
#输出结果
write.csv(result,file="myresult.csv")
write.xlsx(result,file="myresult.xlsx")
write.table(result,file="myresult.txt")

